package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.config.MyRedisTemplate;
import com.woniu.car.dto.StatisticsShopDto;
import com.woniu.car.entity.TStatisticsShop;
import com.woniu.car.exception.StatisticsException;
import com.woniu.car.mapper.TStatisticsShopMapper;
import com.woniu.car.parameter.StatisticsShopParameter;
import com.woniu.car.properties.StatisticsProperties;
import com.woniu.car.service.TStatisticsShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyanzu
 * @since 2021-06-22
 */
@Service
@Slf4j
public class TStatisticsShopServiceImpl extends ServiceImpl<TStatisticsShopMapper, TStatisticsShop> implements TStatisticsShopService {
    @Autowired
    private MyRedisTemplate myRedisTemplate;
    @Override
    public ResponseEntity StatisticsShop(StatisticsShopParameter parameter) throws StatisticsException {
        String shopId = parameter.getShopId().toString();
        if(parameter.getMonth()==null&&parameter.getYear()==null) {
            //redis中如果没有该商铺的今日商品服务数据(未开张)则返回"本日暂无记录"信息
            if (!myRedisTemplate.opsForHash().hasKey(StatisticsProperties.StatisticsShop,shopId))
                throw new StatisticsException(StatisticsProperties.NOGOODSMSG);
            //redis中查询SupplyShop 查看该商铺是否有服务
            //无服务抛出异常"本店铺暂无服务"
            List<String> list = (List<String>) myRedisTemplate.opsForHash().get(StatisticsProperties.SUPPLYSHOP, shopId);
            if(list.size()==0)
                throw new StatisticsException(StatisticsProperties.NOSUPPLYSHOP);
            ArrayList<StatisticsShopDto> arrayList = new ArrayList<>();
            for(String supplyName:list){
                StatisticsShopDto dto = (StatisticsShopDto)myRedisTemplate.opsForHash().get(StatisticsProperties.StatisticsShop+":"+shopId, supplyName);
                arrayList.add(dto);
            }
            return new ResponseEntity()
                    .setCode(StatisticsProperties.SUCCESSCODE)
                    .setMsg(StatisticsProperties.SUCCESSMSG)
                    .setObject(arrayList);
        }else{
            //查询数据库
            String month = parameter.getMonth().toString();
            if(parameter.getMonth()<10)
                month = "0" + month;
            List<TStatisticsShop> list = list(new QueryWrapper<TStatisticsShop>()
                    .ge("time", parameter.getYear().toString() + month + "01")
                    .le("time", parameter.getYear().toString() + month + "31")
            );
            //若集合中无信息则报错
            if(list.size()==0)
                throw new StatisticsException(StatisticsProperties.NOGOODSMSG);
            log.info("查询成功");
            return new ResponseEntity(StatisticsProperties.SUCCESSCODE,StatisticsProperties.SUCCESSMSG,list);
        }
    
    }
}
